package com.example.easyenglish;

import androidx.appcompat.app.AppCompatActivity;

import Connect.ConnectMessage;
import Connect.ConnectToDataBase;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.HashMap;

public class LoginActivity extends AppCompatActivity {
    private EditText etxtUserName;
    private EditText etxtPassword;
    private Button btnLogin;
    private Button btnRegister;
    EditText et_username, et_password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        etxtUserName=(EditText) findViewById(R.id.et_username);
        etxtPassword=(EditText) findViewById(R.id.et_password);

        btnLogin=(Button) findViewById(R.id.btn_login);
        btnRegister=(Button) findViewById(R.id.btn_register);


        findViewById(R.id.btn_register).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
            }
        });

        findViewById(R.id.btn_login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                et_username = findViewById(R.id.et_username);
                et_password = findViewById(R.id.et_password);
                final String name = et_username.getText().toString().trim();
                String password = et_password.getText().toString().trim();
                System.out.println("login");

                if(name.equals("") || password.equals(""))
                    Toast.makeText(LoginActivity.this,"输入不能为空",Toast.LENGTH_SHORT).show();
                else
                    new Thread(commRun).start();

            }
            ConnectMessage a=new ConnectMessage();
            Runnable commRun=new Runnable() {
                @Override
                public void run() {
                    try {

                        System.out.println("trylogin");
                        JSONObject object = new JSONObject();
                        object.put("jsonid", "1");
                        object.put("userphonenumber", etxtUserName.getText().toString());
                        object.put("userpassword", etxtPassword.getText().toString());
                        final String result = object.toString();//json输出部分
                        Log.i("jSON字符串", result);

                        //端口号为6666，自己选择，只要不和电脑中的其他程序冲突即可
                        Socket socket = new Socket(a.GetIpAddress(), 6666);//此处连接后端
                        System.out.println("loginsuccess");
                        OutputStream os = socket.getOutputStream();
                        DataOutputStream out = new DataOutputStream(os);
                        out.writeUTF(result);// 向服务器传送json信息


                        InputStream is1 = socket.getInputStream();
                        DataInputStream in = new DataInputStream(is1);
                        String str = in.readUTF();//读入运行结果
                        object = JSONObject.parseObject(str);//转化为json
                        int status = object.getInteger("status");
                        System.out.println("status:" + status);

                        if (status == 2) {
                            startActivity(new Intent(LoginActivity.this, HomeActivity.class));
                            finish();
                        } else if(status==0)
                            startActivity(new Intent(LoginActivity.this, WrongActivity.class));
                        else if(status==1)
                            startActivity(new Intent(LoginActivity.this, Wrong1Activity.class));
                    }
                    catch(Exception e){
                        String TAG="login";
                        Log.e(TAG,Log.getStackTraceString(e));
                        System.out.println("failed");
                    }
                }
            };
        });

    }
}
